package com.sci99.ex.vm;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.security.core.context.SecurityContextImpl;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.sci99.ex.model.Course;
import com.sci99.ex.model.Department;
import com.sci99.ex.model.Employee;
import com.sci99.ex.service.CourseAssignService;
import com.sci99.ex.service.CourseService;
import com.sci99.ex.service.DptService;

public class CourseDepartmentAssignVM {
	static Logger log = Logger.getLogger(CourseDepartmentAssignVM.class);
	static Logger dblog = Logger.getLogger("db");
	List<Course> courseList;
	List<Department> assignList;
	List<Department> dptList;
	String courseCode;
	String courseName;
	String dptName;
	Set<Department> selAssigns;
	Set<Department> selDpts;
	Course selCourse;

	public List<Department> getDptList() {
		return dptList;
	}

	public void setDptList(List<Department> dptList) {
		this.dptList = dptList;
	}

	public String getDptName() {
		return dptName;
	}

	public void setDptName(String dptName) {
		this.dptName = dptName;
	}

	public Set<Department> getSelAssigns() {
		return selAssigns;
	}

	public void setSelAssigns(Set<Department> selAssigns) {
		this.selAssigns = selAssigns;
	}

	public Course getSelCourse() {
		return selCourse;
	}

	public void setSelCourse(Course selCourse) {
		this.selCourse = selCourse;
	}

	public Set<Department> getSelDpts() {
		return selDpts;
	}

	public void setSelDpts(Set<Department> selDpts) {
		this.selDpts = selDpts;
	}

	public List<Course> getCourseList() {
		return courseList;
	}

	public void setCourseList(List<Course> courseList) {
		this.courseList = courseList;
	}

	public List<Department> getAssignList() {
		return assignList;
	}

	public void setAssignList(List<Department> assignList) {
		this.assignList = assignList;
	}

	public String getCourseCode() {
		return courseCode;
	}

	public void setCourseCode(String courseCode) {
		this.courseCode = courseCode;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	@Command
	@NotifyChange("courseList")
	public void schCourse() {
		try {
			courseList = CourseService.getCourseList(courseName);
			SecurityContextImpl sec = (SecurityContextImpl) Executions
					.getCurrent().getSession()
					.getAttribute("SPRING_SECURITY_CONTEXT");
			Employee emp = (Employee) sec.getAuthentication().getPrincipal();
			dblog.debug(emp.getDepartmentStr() + emp.getRealname());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}

	@Command
	@NotifyChange("assignList")
	public void select() {
		try {
			assignList = DptService.getDptListByCouId(selCourse.getId());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}

	@Command
	@NotifyChange("selAssigns")
	public void selAllAssign() {
		selAssigns = new HashSet<>();
		for (Department d : assignList) {
			selAssigns.add(d);
		}
	}

	@Command
	@NotifyChange("selNodes")
	public void add(@BindingParam("win") Window cWin) {
		if (selCourse == null) {
			Messagebox.show("请选择课程");
			return;
		}
		cWin.doModal();
	}

	@Command
	@NotifyChange("assignList")
	public void del() {
		if (selCourse == null) {
			Messagebox.show("请选择课程");
			return;
		}

		if (Messagebox.show(Msg.CfmDel, null,
				Messagebox.OK + Messagebox.CANCEL, Messagebox.QUESTION) != Messagebox.OK)
			return;
		List<Long> ids = new ArrayList<>();
		for (Department n : selAssigns)
			ids.add(n.getId());
		try {
			CourseAssignService.delCourseDptAssign(selCourse.getId(), ids);
			assignList = DptService.getDptListByCouId(selCourse.getId());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}

	@Command
	@NotifyChange("assignList")
	public void assign(@BindingParam("win") Window cWin) {
		if (selCourse == null) {
			Messagebox.show("请选择课程");
			return;
		}
		try {
			List<Long> ids = new ArrayList<>();
			for (Department n : selDpts)
				ids.add(n.getId());
			CourseAssignService.addCourseDptAssign(selCourse.getId(), ids);
			assignList = DptService.getDptListByCouId(selCourse.getId());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
		cWin.setVisible(false);
	}

	@Command
	@NotifyChange("dptList")
	public void schDpt() {
		try {
			if (assignList != null) {
				dptList = DptService.getDepartmentWithout(assignList, dptName);
			} else {
				dptList = DptService.getDepartment(dptName);
			}
		} catch (Exception e) {

		}
	}

	@Command
	public void close(@BindingParam("win") Window cWin) {
		cWin.setVisible(false);
	}
}
